package su.test.bidirectionalEncryption.symmetricEncryption;

import javax.crypto.*;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @author SuZuQi
 * @title: EncryAES
 * @projectName myEncry
 * @description: 高级加密标准Rijndael加密法，是美国联邦政府采用的一种区块加密标准
 * @date 2019/12/17
 */
public class EncryAES  {

    private static KeyGenerator keyGenerator ;

    private static SecretKey secretKey ;

    private static Cipher cipher ;

    static {
        try {
            keyGenerator = KeyGenerator.getInstance("AES");
            secretKey = keyGenerator.generateKey() ;
            cipher = Cipher.getInstance("AES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }

    public static byte[] encry(String text) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);
     return    cipher.doFinal(text.getBytes());
    }

    public static byte[] decry(byte[] encryBy) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        cipher.init(Cipher.DECRYPT_MODE,secretKey);
        return cipher.doFinal(encryBy);
    }

    public static void main (String[] args ) throws BadPaddingException, InvalidKeyException, IllegalBlockSizeException {
        String love = "I love Huang ChunYuan" ;
        System.out.println(new String(encry(love)));
        System.out.println(new String(decry(encry(love))));
    }
}
